﻿
namespace bookMaster.AssetSystem.Windows.Forms
{
	/// <summary>
	/// abstract Recursos typed datagridview
	/// </summary>
	[System.ComponentModel.DesignerCategoryAttribute("code")]
	public abstract class RecursosDataGridViewBase : System.Windows.Forms.UserControl
	{	
		/// <summary> 
		/// Thrown when the current Entity changed.
		/// </summary>
		public event System.EventHandler<RecursosDataGridViewEventArgs> CurrentEntityChanged;
		
		#region Fields
				
		/// <summary> 
		/// the current entity.
		/// </summary>
		private Entities.Recursos _currentRecursos = null;
		//private Dictionary<string, string> _columnsHeaders = new Dictionary<string, string>();
		
		/// <summary> 
		/// the datagridview instance
		/// </summary>
		protected System.Windows.Forms.DataGridView uxRecursosDataGridView;
		
		/// <summary> 
		/// the errorprovider
		/// </summary>
		protected System.Windows.Forms.ErrorProvider uxRecursosErrorProvider;
		
		/// <summary> 
		/// the bindingsource
		/// </summary>
		protected System.Windows.Forms.BindingSource uxRecursosBindingSource;
		
		
		/// <summary> 
		/// the DGV column associated with the IdRecurso property
		/// </summary>
		protected System.Windows.Forms.DataGridViewTextBoxColumn uxIdRecursoDataGridViewColumn;
		
		/// <summary> 
		/// the DGV column associated with the LlaveRecurso property
		/// </summary>
		protected System.Windows.Forms.DataGridViewTextBoxColumn uxLlaveRecursoDataGridViewColumn;
		
		/// <summary> 
		/// the DGV column associated with the NombreRecurso property
		/// </summary>
		protected System.Windows.Forms.DataGridViewTextBoxColumn uxNombreRecursoDataGridViewColumn;
		
		/// <summary> 
		/// the DGV column associated with the DescripcionRecurso property
		/// </summary>
		protected System.Windows.Forms.DataGridViewTextBoxColumn uxDescripcionRecursoDataGridViewColumn;
		/// <summary> 
		/// the DGV column associated with the TipoRecurso property
		/// </summary>
		protected System.Windows.Forms.DataGridViewComboBoxColumn uxTipoRecursoDataGridViewColumn;
		
		/// <summary> 
		/// the DGV column associated with the PadreRecurso property
		/// </summary>
		protected System.Windows.Forms.DataGridViewTextBoxColumn uxPadreRecursoDataGridViewColumn;
		
		#endregion
					
		#region Combo list
		
				
		private Entities.TList<Entities.TiposRecursos> _TipoRecursoList;
		
		/// <summary> 
		/// The list of selectable TiposRecursos
		/// </summary>
		[System.ComponentModel.Category("DropDownList BindingSources")]
		public Entities.TList<Entities.TiposRecursos> TipoRecursoList
		{
			get {return this._TipoRecursoList;}
			set 
			{
				this._TipoRecursoList = value;
				this.uxTipoRecursoDataGridViewColumn.DataSource = null;
				this.uxTipoRecursoDataGridViewColumn.DataSource = this._TipoRecursoList;
			}
		}
		
		private bool _allowNewItemInTipoRecursoList;
		
		/// <summary> 
		/// Indicates if user can add an item in the list of TiposRecursos
		/// </summary>
		[System.ComponentModel.Category(".netTiers"), System.ComponentModel.Description("Indicates if end user can add a new item in the combobox. an event will be thrown then.")]
		public bool AllowNewItemInTipoRecursoList
		{
			get { return _allowNewItemInTipoRecursoList;}
			set
			{
				this._allowNewItemInTipoRecursoList = value;
				this.uxTipoRecursoDataGridViewColumn.DisplayStyle = value ? System.Windows.Forms.DataGridViewComboBoxDisplayStyle.ComboBox : System.Windows.Forms.DataGridViewComboBoxDisplayStyle.DropDownButton;
			}
		}
		
		#endregion
		
		#region Main Datasource
		
		private Entities.TList<Entities.Recursos> _RecursosList;
				
		/// <summary> 
		/// The list of Recursos to be displayed in the DGV.
		/// </summary>
		[System.ComponentModel.Category("Data")]
		public Entities.TList<Entities.Recursos> RecursosList
		{
			get {return this._RecursosList;}
			set
			{
				this._RecursosList = value;
				this.uxRecursosBindingSource.DataSource = null;
				this.uxRecursosBindingSource.DataSource = value;
				this.uxRecursosDataGridView.DataSource = null;
				this.uxRecursosDataGridView.DataSource = this.uxRecursosBindingSource;				
				//this.uxRecursosBindingSource.ListChanged += new System.ComponentModel.ListChangedEventHandler(uxRecursosBindingSource_ListChanged);
				this.uxRecursosBindingSource.CurrentItemChanged += new System.EventHandler(OnRecursosBindingSourceCurrentItemChanged);
			}
		}
		
		private void OnRecursosBindingSourceCurrentItemChanged(object sender, System.EventArgs e)
		{
			_currentRecursos = uxRecursosBindingSource.Current as Entities.Recursos;
			
			if (_currentRecursos != null)
			{
				_currentRecursos.Validate();
			}
			//_Recursos.Validate();
			OnCurrentEntityChanged();
		}

		//void uxRecursosBindingSource_ListChanged(object sender, System.ComponentModel.ListChangedEventArgs e)
		//{
			//throw new Exception("The method or operation is not implemented.");
		//}		
		
		#endregion
		
		/// <summary>
		/// Gets the currently selected <c cref="Entities.Recursos"/> instance.
		/// </summary>
		public Entities.Recursos SelectedRecursos
		{
			get {return this._currentRecursos;}
		}
		
		#region other properties
		
		/// <summary>
		/// Gets or sets a value indicating whether you have provided your own data-management operations for the DataGridView control.
		/// </summary>
		[System.ComponentModel.Category(".netTiers")]
		public bool VirtualMode
		{
			get {return this.uxRecursosDataGridView.VirtualMode;}
			set
			{
				this.uxRecursosDataGridView.VirtualMode = value;
			}
		}
		
		/// <summary>
		/// Gets or sets a value indicating whether the option to add rows is displayed to the user.
		/// </summary>
		[System.ComponentModel.Category(".netTiers")]
		public bool AllowUserToAddRows
		{
			get {return this.uxRecursosDataGridView.AllowUserToAddRows;}
			set {this.uxRecursosDataGridView.AllowUserToAddRows = value;}
		}
		
		/// <summary>
		/// Gets or sets a value indicating whether the user is allowed to delete rows from the DataGridView.
		/// </summary>
		[System.ComponentModel.Category(".netTiers")]
		public bool AllowUserToDeleteRows
		{
			get {return this.uxRecursosDataGridView.AllowUserToDeleteRows;}
			set {this.uxRecursosDataGridView.AllowUserToDeleteRows = value;}
		}
		
		/// <summary>
		/// Gets the <c cref="DataGridView"/> instance.
		/// </summary>
		[System.ComponentModel.Category(".netTiers")]
		public System.Windows.Forms.DataGridView DataGridView
		{
			get {return this.uxRecursosDataGridView;}
		}
		
		/// <summary>
		/// Gets a collection that contains all the columns in the control.
		/// </summary>
		[System.ComponentModel.Category(".netTiers")]		
		[System.ComponentModel.DesignerSerializationVisibilityAttribute(System.ComponentModel.DesignerSerializationVisibility.Visible)]
		//[System.ComponentModel.EditorAttribute("System.Windows.Forms.Design.DataGridViewColumnCollectionEditor, System.Design", typeof(System.Drawing.Design.UITypeEditor))] 
		public System.Windows.Forms.DataGridViewColumnCollection Columns
		{
			get { return this.uxRecursosDataGridView.Columns; }
		}
		
		#endregion
		
		/// <summary> 
		/// Required designer variable.
		/// </summary>
		private System.ComponentModel.IContainer components = null;
	
		/// <summary> 
		/// Clean up any resources being used.
		/// </summary>
		/// <param name="disposing">true if managed resources should be disposed; otherwise, false.</param>
		protected override void Dispose(bool disposing)
		{
			if (disposing && (components != null))
			{
				components.Dispose();
			}
			base.Dispose(disposing);
		}
	
		#region Constructor
	
		/// <summary>
		/// Initializes a new instance of the <see cref="RecursosDataGridViewBase"/> class.
		/// </summary>
		public RecursosDataGridViewBase()
		{			
			this.components = new System.ComponentModel.Container();			
			
			this.uxRecursosDataGridView = new System.Windows.Forms.DataGridView();
			this.uxRecursosBindingSource = new System.Windows.Forms.BindingSource(this.components);
			this.uxRecursosErrorProvider = new System.Windows.Forms.ErrorProvider(this.components);
			this.uxIdRecursoDataGridViewColumn = new System.Windows.Forms.DataGridViewTextBoxColumn();
			this.uxLlaveRecursoDataGridViewColumn = new System.Windows.Forms.DataGridViewTextBoxColumn();
			this.uxNombreRecursoDataGridViewColumn = new System.Windows.Forms.DataGridViewTextBoxColumn();
			this.uxDescripcionRecursoDataGridViewColumn = new System.Windows.Forms.DataGridViewTextBoxColumn();
			this.uxTipoRecursoDataGridViewColumn = new System.Windows.Forms.DataGridViewComboBoxColumn();
			this.uxPadreRecursoDataGridViewColumn = new System.Windows.Forms.DataGridViewTextBoxColumn();
			//this.uxTipoRecursoBindingSource = new TiposRecursosBindingSource(this.components);
			//((System.ComponentModel.ISupportInitialize)(this.uxTipoRecursoBindingSource)).BeginInit();
			((System.ComponentModel.ISupportInitialize)(this.uxRecursosDataGridView)).BeginInit();
			((System.ComponentModel.ISupportInitialize)(this.uxRecursosBindingSource)).BeginInit();
			((System.ComponentModel.ISupportInitialize)(this.uxRecursosErrorProvider)).BeginInit();
			this.SuspendLayout();
						
			// 
			// uxRecursosErrorProvider
			// 
			this.uxRecursosErrorProvider.ContainerControl = this;
			this.uxRecursosErrorProvider.DataSource = this.uxRecursosBindingSource;						
			// 
			// uxRecursosDataGridView
			// 
			this.uxRecursosDataGridView.AutoGenerateColumns = false;
			this.uxRecursosDataGridView.ColumnHeadersHeightSizeMode = System.Windows.Forms.DataGridViewColumnHeadersHeightSizeMode.AutoSize;
			this.uxRecursosDataGridView.Columns.AddRange(new System.Windows.Forms.DataGridViewColumn[] {
		this.uxIdRecursoDataGridViewColumn,
		this.uxLlaveRecursoDataGridViewColumn,
		this.uxNombreRecursoDataGridViewColumn,
		this.uxDescripcionRecursoDataGridViewColumn,
		this.uxTipoRecursoDataGridViewColumn,
		this.uxPadreRecursoDataGridViewColumn			});
			this.uxRecursosDataGridView.Dock = System.Windows.Forms.DockStyle.Fill;
			this.uxRecursosDataGridView.Location = new System.Drawing.Point(0, 0);
			this.uxRecursosDataGridView.Name = "uxRecursosDataGridView";
			this.uxRecursosDataGridView.Size = new System.Drawing.Size(470, 300);
			this.uxRecursosDataGridView.TabIndex = 0;	
			this.uxRecursosDataGridView.BackgroundColor = System.Drawing.Color.WhiteSmoke;
			this.uxRecursosDataGridView.EnableHeadersVisualStyles = false;
			this.uxRecursosDataGridView.DataError += new System.Windows.Forms.DataGridViewDataErrorEventHandler(this.OnRecursosDataGridViewDataError);
			this.uxRecursosDataGridView.CellValueNeeded += new System.Windows.Forms.DataGridViewCellValueEventHandler(this.OnRecursosDataGridViewCellValueNeeded);
			this.uxRecursosDataGridView.CellValuePushed += new System.Windows.Forms.DataGridViewCellValueEventHandler(this.OnRecursosDataGridViewCellValuePushed);
			
			//
			// uxIdRecursoDataGridViewColumn
			//
			this.uxIdRecursoDataGridViewColumn.AutoSizeMode = System.Windows.Forms.DataGridViewAutoSizeColumnMode.DisplayedCells;
			this.uxIdRecursoDataGridViewColumn.DataPropertyName = "IdRecurso";
			this.uxIdRecursoDataGridViewColumn.HeaderText = "IdRecurso";
			this.uxIdRecursoDataGridViewColumn.Name = "uxIdRecursoDataGridViewColumn";
			this.uxIdRecursoDataGridViewColumn.Resizable = System.Windows.Forms.DataGridViewTriState.True;
			this.uxIdRecursoDataGridViewColumn.SortMode = System.Windows.Forms.DataGridViewColumnSortMode.Automatic;
			this.uxIdRecursoDataGridViewColumn.ReadOnly = true;		
			//
			// uxLlaveRecursoDataGridViewColumn
			//
			this.uxLlaveRecursoDataGridViewColumn.AutoSizeMode = System.Windows.Forms.DataGridViewAutoSizeColumnMode.DisplayedCells;
			this.uxLlaveRecursoDataGridViewColumn.DataPropertyName = "LlaveRecurso";
			this.uxLlaveRecursoDataGridViewColumn.HeaderText = "LlaveRecurso";
			this.uxLlaveRecursoDataGridViewColumn.Name = "uxLlaveRecursoDataGridViewColumn";
			this.uxLlaveRecursoDataGridViewColumn.Resizable = System.Windows.Forms.DataGridViewTriState.True;
			this.uxLlaveRecursoDataGridViewColumn.SortMode = System.Windows.Forms.DataGridViewColumnSortMode.Automatic;
			this.uxLlaveRecursoDataGridViewColumn.ReadOnly = false;		
			//
			// uxNombreRecursoDataGridViewColumn
			//
			this.uxNombreRecursoDataGridViewColumn.AutoSizeMode = System.Windows.Forms.DataGridViewAutoSizeColumnMode.DisplayedCells;
			this.uxNombreRecursoDataGridViewColumn.DataPropertyName = "NombreRecurso";
			this.uxNombreRecursoDataGridViewColumn.HeaderText = "NombreRecurso";
			this.uxNombreRecursoDataGridViewColumn.Name = "uxNombreRecursoDataGridViewColumn";
			this.uxNombreRecursoDataGridViewColumn.Resizable = System.Windows.Forms.DataGridViewTriState.True;
			this.uxNombreRecursoDataGridViewColumn.SortMode = System.Windows.Forms.DataGridViewColumnSortMode.Automatic;
			this.uxNombreRecursoDataGridViewColumn.ReadOnly = false;		
			//
			// uxDescripcionRecursoDataGridViewColumn
			//
			this.uxDescripcionRecursoDataGridViewColumn.AutoSizeMode = System.Windows.Forms.DataGridViewAutoSizeColumnMode.DisplayedCells;
			this.uxDescripcionRecursoDataGridViewColumn.DataPropertyName = "DescripcionRecurso";
			this.uxDescripcionRecursoDataGridViewColumn.HeaderText = "DescripcionRecurso";
			this.uxDescripcionRecursoDataGridViewColumn.Name = "uxDescripcionRecursoDataGridViewColumn";
			this.uxDescripcionRecursoDataGridViewColumn.Resizable = System.Windows.Forms.DataGridViewTriState.True;
			this.uxDescripcionRecursoDataGridViewColumn.SortMode = System.Windows.Forms.DataGridViewColumnSortMode.Automatic;
			this.uxDescripcionRecursoDataGridViewColumn.ReadOnly = false;		
			//
			// uxTipoRecursoDataGridViewColumn
			//
			this.uxTipoRecursoDataGridViewColumn.AutoSizeMode = System.Windows.Forms.DataGridViewAutoSizeColumnMode.DisplayedCells;
			this.uxTipoRecursoDataGridViewColumn.DataPropertyName = "TipoRecurso";
			this.uxTipoRecursoDataGridViewColumn.HeaderText = "TipoRecurso";
			this.uxTipoRecursoDataGridViewColumn.Name = "uxTipoRecursoDataGridViewColumn";
			this.uxTipoRecursoDataGridViewColumn.Resizable = System.Windows.Forms.DataGridViewTriState.True;
			this.uxTipoRecursoDataGridViewColumn.SortMode = System.Windows.Forms.DataGridViewColumnSortMode.Automatic;
			this.uxTipoRecursoDataGridViewColumn.ReadOnly = false;		
			//
			// uxPadreRecursoDataGridViewColumn
			//
			this.uxPadreRecursoDataGridViewColumn.AutoSizeMode = System.Windows.Forms.DataGridViewAutoSizeColumnMode.DisplayedCells;
			this.uxPadreRecursoDataGridViewColumn.DataPropertyName = "PadreRecurso";
			this.uxPadreRecursoDataGridViewColumn.HeaderText = "PadreRecurso";
			this.uxPadreRecursoDataGridViewColumn.Name = "uxPadreRecursoDataGridViewColumn";
			this.uxPadreRecursoDataGridViewColumn.Resizable = System.Windows.Forms.DataGridViewTriState.True;
			this.uxPadreRecursoDataGridViewColumn.SortMode = System.Windows.Forms.DataGridViewColumnSortMode.Automatic;
			this.uxPadreRecursoDataGridViewColumn.ReadOnly = false;		
			//
			// uxTipoRecursoDataGridViewColumn
			//				
			this.uxTipoRecursoDataGridViewColumn.DisplayMember = "DescripcionTipoRecurso";	
			this.uxTipoRecursoDataGridViewColumn.ValueMember = "TipoRecurso";	
			this.uxTipoRecursoDataGridViewColumn.DisplayStyleForCurrentCellOnly = true;
			//uxTipoRecursoDataGridViewColumn.DataSource = uxTipoRecursoBindingSource;				
				
			
			// this
			this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);
			this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
			this.Controls.Add(this.uxRecursosDataGridView);
			this.Name = "RecursosDataGridView";
			this.Size = new System.Drawing.Size(470, 300);
			//((System.ComponentModel.ISupportInitialize)(this.uxTipoRecursoBindingSource)).EndInit();
			((System.ComponentModel.ISupportInitialize)(this.uxRecursosErrorProvider)).EndInit();
			((System.ComponentModel.ISupportInitialize)(this.uxRecursosDataGridView)).EndInit();
			((System.ComponentModel.ISupportInitialize)(this.uxRecursosBindingSource)).EndInit();
			this.ResumeLayout(false);
		}
		#endregion
				
		#region events
		
		/// <summary>
		/// Raised the CurrentEntityChanged event.
		/// </summary>
		protected void OnCurrentEntityChanged()
		{
			if (CurrentEntityChanged != null)
			{
				RecursosDataGridViewEventArgs args = new RecursosDataGridViewEventArgs();
				args.Recursos = _currentRecursos;
				CurrentEntityChanged(this, args); 
			}
		}
		
		#endregion
		
		/// <summary>
		/// Strongly typed event 
		/// </summary>
		public class RecursosDataGridViewEventArgs : System.EventArgs
		{
			private Entities.Recursos	_Recursos;
	
			/// <summary>
			/// the  Entities.Recursos instance.
			/// </summary>
			public Entities.Recursos Recursos
			{
				get { return _Recursos; }
				set { _Recursos = value; }
			}	
		}
		
		/// <summary>
		/// Handles the DataError event of the uxRecursosDataGridView control.
		/// </summary>
		/// <param name="sender">The source of the event.</param>
		/// <param name="e">The <see cref="T:System.Windows.Forms.DataGridViewDataErrorEventArgs"/> instance containing the event data.</param>
		protected void OnRecursosDataGridViewDataError(object sender, System.Windows.Forms.DataGridViewDataErrorEventArgs e)
		{
			System.Diagnostics.Debug.WriteLine(string.Format( "{0}, {1}", e.ColumnIndex, e.RowIndex));
		}
		
		/// <summary>
		/// Handles the CellValueNeeded event of the uxRecursosDataGridView control.
		/// </summary>
		/// <param name="sender">The source of the event.</param>
		/// <param name="e">The <see cref="T:System.Windows.Forms.DataGridViewCellValueEventArgs"/> instance containing the event data.</param>
		protected void OnRecursosDataGridViewCellValueNeeded(object sender, System.Windows.Forms.DataGridViewCellValueEventArgs e)
		{
			switch(uxRecursosDataGridView.Columns[e.ColumnIndex].Name)
			{
					case "uxIdRecursoDataGridViewColumn":
						e.Value = RecursosList[e.RowIndex].IdRecurso;
						break;
					case "uxLlaveRecursoDataGridViewColumn":
						e.Value = RecursosList[e.RowIndex].LlaveRecurso;
						break;
					case "uxNombreRecursoDataGridViewColumn":
						e.Value = RecursosList[e.RowIndex].NombreRecurso;
						break;
					case "uxDescripcionRecursoDataGridViewColumn":
						e.Value = RecursosList[e.RowIndex].DescripcionRecurso;
						break;
					case "uxTipoRecursoDataGridViewColumn":
						e.Value = RecursosList[e.RowIndex].TipoRecurso;
						break;
					case "uxPadreRecursoDataGridViewColumn":
						e.Value = RecursosList[e.RowIndex].PadreRecurso;
						break;
				default:
				break;
			}
		}
		
		/// <summary>
		/// Handles the CellValuePushed event of the uxRecursosDataGridView control.
		/// </summary>
		/// <param name="sender">The source of the event.</param>
		/// <param name="e">The <see cref="T:System.Windows.Forms.DataGridViewCellValueEventArgs"/> instance containing the event data.</param>
		protected void OnRecursosDataGridViewCellValuePushed(object sender, System.Windows.Forms.DataGridViewCellValueEventArgs e)
		{
			switch(uxRecursosDataGridView.Columns[e.ColumnIndex].Name)
			{
					case "uxIdRecursoDataGridViewColumn":
						RecursosList[e.RowIndex].IdRecurso = (int)e.Value;
						break;
					case "uxLlaveRecursoDataGridViewColumn":
						RecursosList[e.RowIndex].LlaveRecurso = (string)e.Value;
						break;
					case "uxNombreRecursoDataGridViewColumn":
						RecursosList[e.RowIndex].NombreRecurso = (string)e.Value;
						break;
					case "uxDescripcionRecursoDataGridViewColumn":
						RecursosList[e.RowIndex].DescripcionRecurso = (string)e.Value;
						break;
					case "uxTipoRecursoDataGridViewColumn":
						RecursosList[e.RowIndex].TipoRecurso = (int)e.Value;
						break;
					case "uxPadreRecursoDataGridViewColumn":
						RecursosList[e.RowIndex].PadreRecurso = (string)e.Value;
						break;
				default:
				break;
			}
		}
	}
}
